// pages/editProfile/editProfile.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    avatarurl: '', // 用户头像URL
    username: '' // 用户昵称
  },
  onLoad: function() {
    // 页面加载时获取当前用户信息
    const avatarurl = defaultAvatarUrl;
    this.setData({
      avatarurl: avatarurl
    });
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.setData({
      "avatarurl": avatarUrl
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    this.setData({
      "username": nickName
    })
  },
  submitProfile: function() {
    // 提交编辑的个人资料
    const self =this;
    const { avatarurl, username } = this.data;
    if (!username) {
      wx.showToast({
        title: '昵称不能为空',
        icon: 'none'
      });
      return;
    }
    wx.uploadFile({
      url: "https://zdw666.fun/wjy/userInfoUpdata",
      // 开发者服务器URL
      filePath: avatarurl,
      // 要上传文件资源的路径
      name: "avatar",
      // 必填，后台用来解析的文件名
      formData: {
        "nickName": username,
        "openId":wx.getStorageSync("openid"),
        "ID":wx.getStorageSync('id')
      },
      success(res) {
        if(res.statusCode==200){
          const data = JSON.parse(res.data)
          console.log(data.data)
          wx.setStorageSync('avatarurl', data.data.avatarurl)
          wx.setStorageSync('username', data.data.username)
          wx.showToast({
            title: '更新成功'
          });
          // 返回“我的”页面
          setTimeout(() => {
            wx.navigateBack();
          }, 2000);
        }else{
          wx.showToast({
            title: '更新失败'
          });
        }
       
      },fail(uploadErr) {
        console.log(uploadErr)
        wx.showToast({
          title: '更新失败'
        });
      }
    });
  }
});